/* Main Application */

#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "drivers/gpio.h"
#include "drivers/uart.h"
#include "drivers/flash.h"
#include "drivers/wifi.h"
#include "drivers/fs.h"
#include "drivers/cli.h"
#include "network/net_init.h"
#include "esp_vfs_dev.h"

/* Version information */
#define VERSION "1.0.0"
#define BUILD_TIME __DATE__ " " __TIME__

/* Task priorities */
#define MAIN_TASK_PRIORITY 5
#define LED_TASK_PRIORITY 3

/* LED blink interval */
#define LED_BLINK_INTERVAL_MS 1000

/* UART configuration */
static uart_config_t uart_config = {
    .baud_rate = UART_BAUD_RATE_115200,
    .data_bits = UART_DATA_8_BITS,
    .parity = UART_PARITY_DISABLE,
    .stop_bits = UART_STOP_BITS_1
};

/* WiFi configuration */
static wifi_sta_config_t wifi_sta_config = {
    .ssid = "TestAP",
    .password = "password123",
    .authmode = WIFI_AUTH_WPA2_PSK,
    .channel = 0,
    .bssid_set = 0
};

/* LED blink task */
static void led_blink_task(void *arg)
{
    printf("LED blink task started\n");
    
    /* Initialize GPIO */
    gpio_init();
    
    /* Set GPIO 2 as output (LED) */
    gpio_set_mode(GPIO_NUM_2, GPIO_MODE_OUTPUT);
    
    while (1) {
        /* Toggle LED */
        gpio_set_level(GPIO_NUM_2, 1);
        vTaskDelay(LED_BLINK_INTERVAL_MS / portTICK_PERIOD_MS);
        
        gpio_set_level(GPIO_NUM_2, 0);
        vTaskDelay(LED_BLINK_INTERVAL_MS / portTICK_PERIOD_MS);
    }
}

/* Main task */
static void main_task(void *arg)
{
    printf("Main task started\n");
    
    /* Initialize UART */
    uart_init(UART_NUM_0, &uart_config);
    printf("UART initialized\n");
    
    /* Configure UART VFS to redirect stdin/stdout */
    esp_vfs_dev_uart_use_driver(UART_NUM_0);
    esp_vfs_dev_uart_port_set_rx_line_endings(UART_NUM_0, ESP_LINE_ENDINGS_CR);
    esp_vfs_dev_uart_port_set_tx_line_endings(UART_NUM_0, ESP_LINE_ENDINGS_CRLF);
    printf("UART VFS configured\n");
    
    /* Initialize Flash */
    flash_init();
    printf("Flash initialized\n");
    
    /* Initialize WiFi */
    wifi_init();
    printf("WiFi initialized\n");
    
    /* Set WiFi mode to STA */
    wifi_set_mode(WIFI_MODE_STA);
    
    /* Connect to WiFi AP */
    wifi_sta_connect(&wifi_sta_config);
    
    /* Initialize network */
    network_init();
    
    /* Initialize file system */
    fs_init();
    printf("File system initialized\n");
    
    /* Initialize CLI */
    cli_init();
    printf("CLI initialized\n");
    
    /* Create LED blink task */
    xTaskCreate(led_blink_task, "led_blink_task", 512, NULL, LED_TASK_PRIORITY, NULL);
    
    /* CLI command line loop */
    while (1) {
        /* Print prompt */
        printf("rteme> ");
        
        /* Read command line */
        char line[256] = {0};
        fgets(line, sizeof(line), stdin);
        
        /* Remove newline character */
        size_t len = strlen(line);
        if (len > 0 && line[len - 1] == '\n') {
            line[len - 1] = '\0';
        }
        
        /* Execute command */
        cli_process_input(line, NULL, 0);
    }
}

/* Application entry point */
void app_main(void)
{
    printf("Hello from ESP32C3 FreeRTOS Project!\n");
    printf("RtemeBSD Version: %s\n", VERSION);
    printf("Build Time: %s\n", BUILD_TIME);
    
    /* Create main task with increased stack size */
    xTaskCreate(main_task, "main_task", 4096, NULL, MAIN_TASK_PRIORITY, NULL);
    
    /* ESP-IDF automatically starts the scheduler */
}
